package impl

import (
	"mvip-route/config"
	"mvip-route/service/model"
)

type DamRedCrudService struct {
}

func (s *DamRedCrudService) CreateDameRed(add model.DamRedVo) error {
	err := config.DB.Create(&add).Error
	return err
}

func (s *DamRedCrudService) GetDameReds() ([]model.DamRedVo, error) {
	var dameReds []model.DamRedVo
	err := config.DB.Order("id desc").Find(&dameReds).Error
	return dameReds, err
}

func (s *DamRedCrudService) UpdateDameRed(up model.DamRedVo, id int) error {
	updates := make(map[string]interface{})
	updates["state"] = up.State
	if up.Platform != "" {
		updates["platform"] = up.Platform
	}
	if up.Target != "" {
		updates["target"] = up.Target
	}
	if up.Sld != "" {
		updates["sld"] = up.Sld
	}
	if up.Domain != "" {
		updates["domain"] = up.Domain
	}
	err := config.DB.Model(&model.DamRedVo{}).Where("id = ?", id).Updates(&updates).Error
	return err
}

func (s *DamRedCrudService) DeleteDameRed(id int) error {
	return config.DB.Model(&model.DamRedVo{}).Where("id = ?", id).Update("deleted", 1).Error
}
